<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>向生成器中传入一个值</title>
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-base.css" />
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-medium.css" />

 </head>
 <body class="docs"><div class="navbar navbar-fixed-top">
  <div class="navbar-inner clearfix">
    <ul class="nav" style="width: 100%">
      <li style="float: left;"><a href="generator.rewind.html">« Generator::rewind</a></li>
      <li style="float: right;"><a href="generator.throw.html">Generator::throw »</a></li>
    </ul>
  </div>
</div>
<div id="breadcrumbs" class="clearfix">
  <ul class="breadcrumbs-container">
    <li><a href="index.html">PHP Manual</a></li>
    <li><a href="class.generator.html">Generator</a></li>
    <li>向生成器中传入一个值</li>
  </ul>
</div>
<div id="layout">
  <div id="layout-content"><div id="generator.send" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">Generator::send</h1>
  <p class="verinfo">(PHP 5 &gt;= 5.5.0, PHP 7, PHP 8)</p><p class="refpurpose"><span class="refname">Generator::send</span> &mdash; <span class="dc-title">向生成器中传入一个值</span></p>

 </div>

 <div class="refsect1 description" id="refsect1-generator.send-description">
  <h3 class="title">说明</h3>
  <div class="methodsynopsis dc-description"><span class="modifier">public</span> <span class="methodname"><strong>Generator::send</strong></span>(<span class="methodparam"><span class="type"><a href="language.types.declarations.html#language.types.declarations.mixed" class="type mixed">mixed</a></span> <code class="parameter">$value</code></span>): <span class="type"><a href="language.types.declarations.html#language.types.declarations.mixed" class="type mixed">mixed</a></span></div>

  <p class="para rdfs-comment">
   向生成器中传入一个值，并且当做 <a href="language.generators.syntax.html#control-structures.yield" class="link">yield</a> 表达式的结果，然后继续执行生成器。
  </p>

  <p class="para">
   如果当这个方法被调用时，生成器不在 <a href="language.generators.syntax.html#control-structures.yield" class="link">yield</a> 表达式，那么在传入值之前，它会先运行到第一个 <a href="language.generators.syntax.html#control-structures.yield" class="link">yield</a> 表达式。
   因此没有必要调用 <span class="methodname"><a href="generator.next.html" class="methodname">Generator::next()</a></span> 让 PHP 生成器 “准备”（就像是 Python 那样做）。
  </p>
 </div>


 <div class="refsect1 parameters" id="refsect1-generator.send-parameters">
  <h3 class="title">参数</h3>
  <dl>
   
    <dt>
<code class="parameter">value</code></dt>

    <dd>

     <p class="para">
      传入生成器的值。这个值将会被作为生成器当前所在的 <a href="language.generators.syntax.html#control-structures.yield" class="link">yield</a> 的返回值
     </p>
    </dd>

   
  </dl>

 </div>

 
 <div class="refsect1 returnvalues" id="refsect1-generator.send-returnvalues">
  <h3 class="title">返回值</h3>
  <p class="para">
   返回生成的值。
  </p>
 </div>


 <div class="refsect1 examples" id="refsect1-generator.send-examples">
  <h3 class="title">范例</h3>
  <p class="para">
   <div class="example" id="example-401">
    <p><strong>示例 #1 用 <span class="methodname"><strong>Generator::send()</strong></span> 向生成器函数中传值</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /></span><span style="color: #007700">function&nbsp;</span><span style="color: #0000BB">printer</span><span style="color: #007700">()&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"I'm&nbsp;printer!"</span><span style="color: #007700">.</span><span style="color: #0000BB">PHP_EOL</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;while&nbsp;(</span><span style="color: #0000BB">true</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$string&nbsp;</span><span style="color: #007700">=&nbsp;yield;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #0000BB">$string</span><span style="color: #007700">.</span><span style="color: #0000BB">PHP_EOL</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br />}<br /><br /></span><span style="color: #0000BB">$printer&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">printer</span><span style="color: #007700">();<br /></span><span style="color: #0000BB">$printer</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">send</span><span style="color: #007700">(</span><span style="color: #DD0000">'Hello&nbsp;world!'</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$printer</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">send</span><span style="color: #007700">(</span><span style="color: #DD0000">'Bye&nbsp;world!'</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

    <div class="example-contents"><p>以上例程会输出：</p></div>
    <div class="example-contents screen">
<div class="cdata"><pre>
I&#039;m printer!
Hello world!
Bye world!
</pre></div>
    </div>
   </div>
  </p>
 </div>

</div></div></div></body></html>